function fblogin() {
	FB.getLoginStatus(function(resp) {
		if (resp.session) {
			var uid = '', accessToken = '';
			fbchecklogin(fbOnLoginFailed, fbOnLoginSuccess, uid, accessToken);
		} else {
			FB.login(function(response) {
				if (response.authResponse) {
					console.log('Welcome!  Fetching your information.... ');
					
					var accessToken = response.authResponse.accessToken;
					var uid = response.authResponse.userID;
					var expiresIn = response.authResponse.expiresIn;
					var signedRequest = response.authResponse.signedRequest;
					
					FB.api('/me', function(response) {
						console.log('Good to see you, ' + response.name + '.');
					});
					
					fbchecklogin(fbOnLoginFailed, fbOnLoginSuccess, uid, accessToken);
				} else {
					console.log('User cancelled login or did not fully authorize.');
				}
			}, { scope:'read_stream,publish_stream,offline_access,user_birthday,email,user_photos' });
		}
	});
}

function fbOnLoginFailed(msg, uid, accessToken) {
	if (msg != null) {
		var message = msg;
	} else {
		var message = 'Your facebook login is not registered.'
	}
	
	if (confirm(message)) {
		//window.location.href = baseurl + 'index.php?route=account/register' + url;
	}
	/*	FB.api({
			method: 'fql.query',
			//query: 'SELECT name,birthday_date,pic_big,email FROM user WHERE uid=' + FB.getSession().uid
			query: 'SELECT name,birthday_date,pic_big,pic_square,email FROM user WHERE uid = me()'
		}, function(response) {
			var name = response[0].name;
			// ...
		});
	} else {
		$('form#registerform input[name="reg"]').val('web');
		$('form#registerform input[name="uid"]').val('');
	}*/
}

function fbOnLoginSuccess() {
	window.location.href = baseurl + 'index.php?route=account/account';
}

function fbchecklogin(onFailed, onSuccess, uid, accessToken) {
	$.ajax({
		url: baseurl+'index.php?route=account/login/facebook',
		//data: 'c=login&by=fb&uid='+FB.getSession().uid+'&access_token='+FB.getSession().access_token,
		data: 'cmd=loginfb&uid='+uid+'&access_token='+accessToken,
		type: 'post',
		dataType: 'json',
		success: function(json) {
			if (json.status == 'failed') {
				//var url = '&uid=' + uid + '&access_token=' + accessToken;
				//onFailed(json.message, url);
				//onFailed(json.message, uid, accessToken);
				
				alert(json.message);
			} else {
				onSuccess();
			}
		}
	});
}